#coding=utf8

from django.shortcuts import render_to_response,render
from ETP.models import Product,Job
from django.db.models import Q
from django.http import HttpResponseRedirect,Http404,HttpResponse
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils import timezone
import json

def products_new(request):
    user = request.user
    products = Product.objects.filter(
        Q(owner=request.user) or Q(member__username__contains=request.user) or Q(member_leader=request.user) or Q(
            participant_leader=request.user) or Q(participant__username__contains=request.user))
    context = {'products':products}
    return render(request,'ETP/product/products.html',context)

def product_new(request,product_id):
    product = Product.objects.filter(id=product_id)
    jobs = Job.objects.filter(product_id=product_id)
    context = {'product':product,'jobs':jobs}
    return render(request,'ETP/product/product.html',context)

def createProduct(request):
    respJson = {
        "code":"0",
        "message":"请求结果信息",
        "data":{}
    }
    if  request.method == 'POST':
        product_name = request.POST.get('productName')
        product_description = request.POST.get('description')
        member_leaderID = request.POST.get('member_leaderId')
        participant_leaderID = request.POST.get('participant_leaderId')
        membersIds = request.POST.getlist('membersIds')
        participantsIds = request.POST.getlist('participantsIds')
        userName = request.POST.get('user')
        user = User.objects.get(username=userName)
        hasProduct = False
        #判断是否存在重名的产品，如果不存在进行添加
        try:
            product = Product.objects.get(name=product_name)
            hasProduct = True
        except Exception as e:
            hasProduct = False

        if not hasProduct:
            memberLeader = User.objects.get(id=member_leaderID)
            participantLeader = User.objects.get(id=participant_leaderID)
            newproduct = Product.objects.create(name=product_name, date_added=timezone.now(),
                                                owner=user, member_leader=memberLeader,
                                                participant_leader=participantLeader, description=product_description)
            for membersId in membersIds:
                member = User.objects.get(id=membersId)
                newproduct.member.add(member)
            for participantsId in participantsIds:
                participant = User.objects.get(id=participantsId)
                newproduct.participant.add(participant)

            respJson = {
                "code": "0",
                "message": "产品添加成功",
                "data": {
                    "redirectURL":'products_new'
                }
            }
            return HttpResponse(json.dumps(respJson,ensure_ascii=False),content_type='application/json,charset=utf-8')
        else:
            respJson = {
                "code": "-9",
                "message": "系统中已经存在相同名称的产品，请更换名称",
                "data": {}
            }
            return HttpResponse(json.dumps(respJson,ensure_ascii=False), content_type='application/json,charset=utf-8')  # 使用json格式进行数据返回